package com.aliyun.messagemgt.application.service.Impl.push.system;

import com.alibaba.cloudapi.sdk.core.model.ApiResponse;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.messagemgt.common.convert.MsgPushVOConverter;
import com.aliyun.messagemgt.common.dto.send.MsgMiddleResult;
import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.common.dto.send.PushChannelInfo;
import com.aliyun.messagemgt.application.service.MsgPushChannel;
import com.aliyun.messagemgt.common.dto.send.SystemMsgPushDTO;
import com.aliyun.messagemgt.common.enums.PushResultEnum;
import com.aliyun.messagemgt.util.SyncApiListClient;
import com.aliyun.messagemgt.common.enums.ChannelTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统推送渠道
 *@Author Znight
 *@Date 14:35 2019/6/12
 */
//@service
public class SystemMsgPushChannel implements MsgPushChannel {
    private static final Logger logger = LoggerFactory.getLogger(SystemMsgPushChannel.class);

    @Override
    public Integer getChannelType() {
        return ChannelTypeEnum.SYSTEM.getType();
    }

    @Override
    public void push(PushChannelInfo pushChannelInfo, MsgRecordVO msgRecordVO) {
        // TODO 逻辑修正，invalidUser为空时 不必发送
        String apiAppKey = pushChannelInfo.getApiAppKey();
        String apiAppSecret = pushChannelInfo.getApiAppSecret();
        String apiUrl = pushChannelInfo.getApiUrl();
        String apiHost = pushChannelInfo.getApiHost();
        ApiResponse apiResponse = null;
        // TODO 发送结果处理
        List<MsgMiddleResult> results = msgRecordVO.getResults();
        SystemMsgPushDTO systemMsgPushDTO = MsgPushVOConverter.INSTANCE.msgRecordVO2systemMsgPushDTO(msgRecordVO);
        try {
            SyncApiListClient syncClient = SyncApiListClient.newBuilder().appKey(apiAppKey).appSecret(apiAppSecret)
                    .connectionTimeoutMillis(30000L).readTimeoutMillis(30000L).writeTimeoutMillis(30000L).build();
            Map<String, String> headerParams = new HashMap<>(16);
            Map<String, String> queryParams = new HashMap<>(16);
            apiResponse = syncClient.apiList(queryParams, headerParams, apiUrl, apiHost, JSONObject.toJSONString(systemMsgPushDTO).getBytes());
            syncClient.shutdown();
        } catch (Exception e) {
            logger.error("系统端消息推送异常", e);
            MsgMiddleResult msgMiddleResult = new MsgMiddleResult().setPushResultCode(PushResultEnum.SYSTEM_PUSH_ERROR.getErrorCode())
                    .setPushResultDescription(PushResultEnum.SYSTEM_PUSH_ERROR.getDescription()).setReceivers(msgRecordVO.getReceivers());
            results.add(msgMiddleResult);
        }
        // 3. 发送结果处理
        Integer errorCode = apiResponse == null ? -1 : apiResponse.getStatusCode();
        if (200 == errorCode) {
            MsgMiddleResult msgMiddleResult = new MsgMiddleResult().setPushResultCode(PushResultEnum.PUSH_SUCCESS.getErrorCode())
                    .setPushResultDescription(PushResultEnum.PUSH_SUCCESS.getDescription()).setReceivers(msgRecordVO.getReceivers());
            results.add(msgMiddleResult);
        } else {
            MsgMiddleResult msgMiddleResult = new MsgMiddleResult().setPushResultCode(PushResultEnum.SYSTEM_PUSH_ERROR.getErrorCode())
                    .setPushResultDescription(PushResultEnum.SYSTEM_PUSH_ERROR.getDescription()).setReceivers(msgRecordVO.getReceivers());
            results.add(msgMiddleResult);
        }

    }
}
